﻿/****************************** Module Header ******************************\
 * Module Name:  ExMethodForDropDownList.cs
 * Module Version:  1.0.0 (2011-9-30)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used to extension webcontrol instance's method.  It supplies 
 * public static extension methods to load data or get othor control.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/

using System;
using System.Collections.Generic;
using System.Data;
using System.Web.UI.WebControls;

namespace UCL.Web
{
    /// <summary>
    /// DropDownList服务器控件的扩展方法
    /// </summary>
    public static class ExMethodForDropDownList
    {
        /// <summary>
        /// DropDownList控件绑定DataTable数据源
        /// </summary>
        /// <param name="ddlControl"></param>
        /// <param name="dataTable"></param>
        public static void LoadData(this DropDownList ddlControl, ref DataTable dataTable)
        {
            ddlControl.DataSource = dataTable;
            ddlControl.DataBind();
        }
        /// <summary>
        /// DropDownList控件绑定DataTable数据源
        /// </summary>
        /// <param name="ddlControl"></param>
        /// <param name="dataTable"></param>
        /// <param name="defaultItem">新增的单元，[0]=value,[1]=text<para>如果[1]=""则使用默认设置{"-1","请选择分类"}</para></param>
        public static void LoadDataAndItem(this DropDownList ddlControl, DataTable dataTable, params string[] defaultItem)
        {
            if (defaultItem.Length == 2)
            {
                if (defaultItem[1].Length == 0)
                {
                    defaultItem[0] = "-1";
                    defaultItem[1] = "请选择分类";
                }
                DataRow oDr = dataTable.NewRow();
                oDr.ItemArray = defaultItem;
                dataTable.Rows.InsertAt(oDr, 0);
            }
            ddlControl.DataSource = dataTable;
            ddlControl.DataBind();
        }
        /// <summary>
        /// DropDownList控件绑定DataTable数据源
        /// </summary>
        /// <param name="ddlControl"></param>
        /// <param name="dataTable"></param>
        public static void LoadDataAndItem(this DropDownList ddlControl, DataTable dataTable)
        {
            ddlControl.LoadDataAndItem(dataTable, "", "");
        }

        /// <summary>
        /// DropDownList控件绑定IList泛型数据源
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="ddlControl"></param>
        /// <param name="iList"></param>
        public static void LoadData<T>(this DropDownList ddlControl, ref IList<T> iList)
        {
            ddlControl.DataSource = iList;
            ddlControl.DataBind();
        }

        /// <summary>
        /// 选择下拉列表中的指定项目
        /// </summary>
        /// <param name="ddlControl"></param>
        /// <param name="value"></param>
        public static void SelectListItem(this DropDownList ddlControl, string value)
        {
            foreach (ListItem li in ddlControl.Items)
            {
                if (value == li.Value)
                {
                    li.Selected = true;
                    break;
                }
            }
        }

        /// <summary>
        /// 初始化下拉列表数据
        /// </summary>
        /// <param name="ddlControl"></param>
        /// <param name="value"></param>
        /// <param name="text"></param>
        public static void InitData(this DropDownList ddlControl, string text, string value)
        {
            ddlControl.Items.Add(new ListItem(text, value));
        }
    }
}
